<template>
  <el-row>
    <h1>随访日记</h1>
    <el-col :span="24">
      <diary-edit ref="DiaryEdit" v-on:save-finished="searchDiary"></diary-edit>
      <el-row class="search-row">
        <el-col :span="12">
          <el-input
            placeholder="姓名"
            size="small"
            v-model="search.diaryPerson"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchDiary()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="12" style="text-align: right">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            v-show="permission.add"
            @click="handleCreateDiary()"
            >添加</el-button
          >
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table
            :data="upDiaryList"
            border
            size="mini"
            header-cell-class-name="header-row"
          >
            <el-table-column
              prop="diaryId"
              label="序号"
              header-align="center"
              align="center"
            >
            </el-table-column>

            <el-table-column
              prop="dateVisit"
              label="回访日期"
              header-align="center"
            >
            </el-table-column>

            <el-table-column
              prop="diaryPerson"
              label="被干预人"
              header-align="center"
            >
            </el-table-column>

            <el-table-column
              prop="returnVisit"
              label="回访人员"
              header-align="center"
            >
            </el-table-column>

            <el-table-column prop="mode" label="回访方式" header-align="center">
            </el-table-column>

            <el-table-column
              prop="contentVisit"
              label="回访内容"
              header-align="center"
            >
            </el-table-column>

            <el-table-column
              prop="feedbackInfomation"
              label="反馈信息"
              header-align="center"
            ></el-table-column>

            <el-table-column
              prop="handle"
              label="处理结果"
              header-align="center"
            >
            </el-table-column>

            <el-table-column
              label="操作"
              header-align="center"
              align="center"
              width="260"
            >
              <template slot-scope="scope">
                <el-button
                  v-show="permission.edit"
                  type="primary"
                  size="mini"
                  @click="handleChangePassword(scope.$index, scope.row)"
                  >编辑</el-button
                >
                <el-button
                  v-show="permission.remove"
                  type="danger"
                  size="mini"
                  @click="handleDeleteDiray(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- 分页模块 -->
      <el-row>
        <el-col :span="24">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="1"
            :page-sizes="[3, 5, 10]"
            :page-size="3"
            layout="total, sizes, prev, pager, next, jumper"
            :total="upDiaryTotal"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import DiaryEdit from "@/components/HealthIntervention/followUpDiary/DiaryEdit";
import {
  findUpDiary,
  removeDiray,
  getDiary,
} from "@/api/HealthIntervention/followUpDiary";

export default {
  data() {
    return {
      loading: false,
      permission: {
        add: true,
        edit: true,
        remove: true,
      },
      search: {
        name: "",
        offset: 1,
        limit: 3,
      },
      upDiaryTotal: 0,
      upDiaryList: [],
    };
  },
  methods: {
    searchDiary() {
      this.loading = true;
      findUpDiary(this.search)
        .then((response) => {
          this.upDiaryTotal = response.data.total;
          this.upDiaryList = response.data.rows;
          this.loading = false;
        })
        .catch((error) => {
          console.log(error);
          this.loading = false;
        });
    },

    handleSizeChange(val) {
      this.search.limit = val;
      this.search.offset = 1;
      this.searchDiary();
    },
    handleCurrentChange(val) {
      this.search.offset = val;
      this.search.currentPage = val;
      this.searchDiary();
    },

    async handleCreateDiary() {
      this.loading = true;
      try {
        this.$refs.DiaryEdit.$emit("openAddDiaryDialog");
        this.loading = false;
      } catch (error) {
        this.outputError(error);
      }
    },

    async handleChangePassword(index, row) {
      this.loading = true;
      try {
        let [getRouterResp] = await Promise.all([getDiary(row.diaryId)]);
        this.$refs.DiaryEdit.$emit("openEditDiaryDialog", getRouterResp.data);
        this.loading = false;
      } catch (error) {
        this.outputError(error);
      }
    },

    handleDeleteDiray(index, row) {
      this.$confirm("确定删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          removeDiray(row.diaryId)
            .then((response) => {
              this.loading = false;
              this.searchDiary();
            })
            .catch((error) => {
              this.outputError(error);
            });
        })
        .catch(() => {});
    },

    async pageInit() {
      this.loading = true;
      try {
        let [searchUserResp] = await Promise.all([findUpDiary(this.search)]);
        this.upDiaryList = searchUserResp.data.rows;
        this.upDiaryTotal = searchUserResp.data.total;
        this.loading = false;
      } catch (error) {
        this.outputError(error);
      }
    },

    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.loading = false;
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error",
      });
    },
  },
  components: { DiaryEdit },
  created() {
    this.pageInit();
  },
};
</script>

<style lang="scss" scoped>
.search-row {
  padding-bottom: 8px;
  .el-input {
    width: 260px;
  }
}

.role-locked {
  color: red;
}
.role-not-locked {
  color: green;
}
</style>


